package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.po.Dept;
import com.itheima.domain.po.Emp;
import com.itheima.domain.po.EmpExpr;
import com.itheima.domain.query.EmpQueryParam;
import com.itheima.domain.vo.PageResult;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import jakarta.annotation.Resource;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Resource
    private DeptMapper deptMapper;
    @Resource
    private EmpExprMapper empExprMapper;

    @Override
    public PageResult<Emp> pageResult(EmpQueryParam param) {
        Integer page = param.getPage();
        Integer pageSize = param.getPageSize();
        if (ObjectUtils.isNull(page) || ObjectUtils.isNull(pageSize)) {
            throw new RuntimeException("page or pageSize is Null");
        }
        Page<Emp> empPage = Page.of(page, pageSize);


//        String name = param.getName();
//        Integer gender = param.getGender();
//        LocalDate begin = param.getBegin();
//        LocalDate end = param.getEnd();
//
//        QueryWrapper<Emp> between = Wrappers.<Emp>query().like(StringUtils.isNotBlank(name), "name", name)
//                .eq(ObjectUtils.isNotNull(gender), "gender", gender)
//                .between(begin != null && end != null && begin.isBefore(end), "entry_date", begin, end).orderByDesc("update_time");
//        this.page(empPage, between);
//
//        this.<Emp>lambdaQuery().like(StringUtils.isNotBlank(name), Emp::getName, name).eq(ObjectUtils.isNotNull(gender), Emp::getGender, gender)
//                .between(begin != null && end != null && begin.isBefore(end), Emp::getEntryDate, begin, end).orderByDesc(Emp::getUpdateTime).page(empPage);
//
//        List<Emp> records = empPage.getRecords();
//        if (CollectionUtils.isEmpty(records)) {
//            return null;
//        }
//        records.forEach(emp -> {
//            Dept dept = deptMapper.selectById(emp.getDeptId());
//            Assert.notNull(dept,"员工关联部门不能为空");
//            emp.setDeptName(dept.getName());
//        });

        return PageResult.<Emp>builder().total(empPage.getTotal()).rows(this.getBaseMapper().pageList(empPage,param)).build();
    }

    @Override
    public Boolean saveEmp(Emp emp) {
        if(ObjectUtils.isNull(emp)){
            return null;
        }
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setPassword("123456");
        List<EmpExpr> exprList = emp.getExprList();
        if(CollectionUtils.isNotEmpty(exprList)){
            exprList.forEach(expr->{
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insert(exprList);
        }
        return this.save(emp);
    }

    @Override
    public Boolean deleteEmp(List<Integer> ids) {
        EmpService proxy = (EmpService) AopContext.currentProxy();
        boolean sucs = proxy.removeBatchByIds(ids);
        if(sucs){
            ids.forEach(id->{
                empExprMapper.delete(Wrappers.<EmpExpr>query().eq("emp_id",id));
            });
        }
        return sucs;
    }

    @Override
    public Emp queryEmpById(Integer id) {
        Emp emp = this.getBaseMapper().selectById(id);
        if(ObjectUtils.isNull(emp)){
            return null;
        }
        Dept dept = deptMapper.selectById(emp.getDeptId());
        emp.setDeptName(dept.getName());
        List<EmpExpr> empExprs = empExprMapper.selectList(Wrappers.<EmpExpr>query().eq("emp_id", emp.getId()));
        emp.setExprList(empExprs);
        return emp;
    }

    @Override
    public Boolean updateEmp(Emp emp) {
        return this.updateById(emp);
    }
}

